package service

import (
	"ksd-social-api/commons/base/service"
	context3 "ksd-social-api/modules/msg/model/context"
	cmodel "ksd-social-api/modules/special/model"
	"strconv"
)

type SpecialCodeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询专题和code是否存在
 */
func (service *SpecialCodeService) DuihuanSpecial(code string, specialid uint64, userId uint64, systemId uint64, systemName string) bool {
	specialCode := specialCodeMapper.CountSpecialCode(code, specialid, systemId)
	// 如果还没兑换，就开始兑换，生成专题订单，同时删除订单
	if specialCode > 0 {
		// 删除兑换的订单
		specialCodeMapper.UpdateSpecialCode(code, specialid, systemId)
		// 开始保存订单
		service.saveUserSpecialOrder(code, specialid, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户专题订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *SpecialCodeService) saveUserSpecialOrder(code string, specialId uint64, userId uint64,
	systemId uint64, systemName string) {
	// 1： 判断用户是否购买此专题，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialMapper.CountUserBuySpecialNo(userId, specialId, systemId)
	if isBuyFlag == 0 {
		// 根据专题id查询专题信息
		special, _ := specialMapper.GetByIdTOPay(specialId, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userSpecialContext := cmodel.UserBuySpecial{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userSpecialContext.Opid = specialId
		userSpecialContext.Title = special.Title
		userSpecialContext.Cover = special.Img

		userSpecialContext.UserId = userId
		userSpecialContext.Username = userVo.Username
		userSpecialContext.Avatar = userVo.Avatar
		userSpecialContext.Uuid = userVo.Uuid
		userSpecialContext.Phone = userVo.Telephone
		userSpecialContext.Address = userVo.Address
		userSpecialContext.Nickname = userVo.Nickname

		userSpecialContext.Code = code
		userSpecialContext.PayMethod = 3
		userSpecialContext.PayMethodName = "兑换码"
		userSpecialContext.Tradeno = OutTradeNo
		userSpecialContext.Orderno = OutTradeNo
		userSpecialContext.OrderJson = ""
		// 保存订单的接口
		userSpecialContext.SystemId = systemId
		userSpecialContext.SystemName = systemName
		userSpecialContext.Price = special.Price
		userBuySpecialMapper.SaveUserBuySpecial(userSpecialContext)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = special.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/special/" + special.Path
		messageMeService.SaveMessageMeBuyResumeTemplate(&messageMeContext)
	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *SpecialCodeService) DuihuanCoinSpecial(specialId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 根据小册id查询小册信息
	special, _ := specialMapper.GetByIdTOPay(specialId, systemId)
	userVo := userMapper.GetUserByID(userId, systemId)
	price, _ := strconv.Atoi(special.Price)
	if userVo.Coin <= 0 || price > userVo.Coin {
		return false
	}
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialMapper.CountUserBuySpecialNo(userId, specialId, systemId)
	if isBuyFlag == 0 {
		userBuySpecial := cmodel.UserBuySpecial{}
		OutTradeNo := service.GetSnowWorkerIdString(10)
		// 处理返回结果
		userBuySpecial.Opid = specialId
		userBuySpecial.Title = special.Title
		userBuySpecial.Cover = special.Img
		userBuySpecial.UserId = userId
		userBuySpecial.Username = userVo.Username
		userBuySpecial.Avatar = userVo.Avatar
		userBuySpecial.Uuid = userVo.Uuid
		userBuySpecial.Phone = userVo.Telephone
		userBuySpecial.Address = userVo.Address
		userBuySpecial.Nickname = userVo.Nickname

		userBuySpecial.Code = ""
		userBuySpecial.PayMethod = 4
		userBuySpecial.PayMethodName = "学习币"
		userBuySpecial.Tradeno = OutTradeNo
		userBuySpecial.Orderno = OutTradeNo
		userBuySpecial.OrderJson = ""
		userBuySpecial.SystemId = systemId
		userBuySpecial.SystemName = systemName
		userBuySpecial.Price = special.Price
		// 保存订单的接口
		userBuySpecialMapper.SaveUserBuySpecialCoin(userBuySpecial)
	}
	// 兑换成功
	return true
}
